package com.nxk.modules.core.web;

import com.nxk.modules.core.entity.SysLog;
import com.nxk.modules.core.entity.SysParam;
import com.nxk.modules.core.entity.SysUser;
import com.nxk.modules.core.entity.vo.MenuRoleVo;
import com.nxk.modules.core.service.SysLogService;
import com.nxk.modules.core.service.SysMenuRoleService;
import com.nxk.modules.core.service.SysParamService;
import com.nxk.modules.exception.RequestStatus;
import com.nxk.modules.exception.ResultEntity;
import com.nxk.modules.utils.DesUtils;
import com.nxk.modules.utils.ListToJson;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/a/core/user")
public class LoginController {

    @Autowired
    private SysParamService sysParamService;

    @Autowired
    private SysMenuRoleService sysMenuRoleService;

    @Autowired
    private SysLogService sysLogService;


    @PostMapping("/login")
    public ResultEntity login(SysUser sysUser, HttpServletRequest request) {
        SysLog sysLog = new SysLog();
         Map<String, Object> res = new HashMap<>();
        //查找加密key
        SysParam sysParam = sysParamService.queryByKey("loginSecretKey");
        //解密用户名和密码
        String loginCode = DesUtils.strDec(sysUser.getLoginCode(), sysParam.getParamValue(), null, null);
        String password = DesUtils.strDec(sysUser.getPassword(), sysParam.getParamValue(), null, null);
        sysLog.setLoginCode(loginCode);
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(loginCode, loginCode + password);
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);

            //当前登陆的用户信息
            SysUser principal = (SysUser) subject.getPrincipal();
            principal.setSalt(null);  //盐值不提供给前端
            principal.setPassword(null);  //密码不提供给前端

            //查找当前用户权限
            List<MenuRoleVo> menuRoleVos = sysMenuRoleService.queryMenuByRoleCode(principal.getUserRole());
            List<MenuRoleVo> menuJson3 = ListToJson.getMenuJson3(menuRoleVos);
            Set<String> permissionSet = new HashSet<>();
            menuRoleVos.stream().forEach(menuRoleVo -> {
                if (MenuRoleVo.ROLE.equals(menuRoleVo.getMenuRole())){
                    permissionSet.add(menuRoleVo.getMenuIdent());
                }
            });

            res.put("sysUser", principal);    //当前用户信息
            res.put("menuRole",menuJson3);  //用户的菜单权限
            res.put("sysRole",permissionSet);  //用户所有权限
            res.put("sessionId", subject.getSession().getId());  //会话id
        } catch (UnknownAccountException e) {
            return ResultEntity.fail(e.getMessage());
        } catch (IncorrectCredentialsException e) {
            return ResultEntity.fail("密码不正确");
        } catch (Exception e) {
            return ResultEntity.fail("未知错误,请联系管理员");
        }
        sysLog.setIsException("2");
        sysLog.setRemarks("正常登陆");
        sysLogService.saveLog(sysLog,request);
        return ResultEntity.success(res);
    }


    @GetMapping("refreshLogin")
    public ResultEntity refreshLogin(){
        return ResultEntity.fail(RequestStatus.EOORE_REFRESH_LOGIN.getCode(),
                RequestStatus.EOORE_REFRESH_LOGIN.getMessage());
    }



}
